# Title     : TODO
# Objective : TODO
# Created by: Administrator
# Created on: 2019/6/18

createWhenNoExist <- function(f){
    ! dir.exists(f) && dir.create(f)
}

library(gplots)
library(Hmisc)
library(optparse)
library(dplyr)
library(ropls)
library(magrittr)
library(tibble)
library(tidyr)
library(scales)

option_list <- list(
make_option("--i", default = "AllMet.csv", type = "character", help = "metabolite data file"),
make_option("--g", default = "SampleInfo.csv", type = "character", help = "sample group file"),
make_option("--sc", default = "sample_color.txt", type = "character", help = "sample color file")
)
opt <- parse_args(OptionParser(option_list = option_list))

sampleIds <- read.csv(opt$g, header = T, stringsAsFactors = F) %>%
    select(c("SampleID", "ClassNote")) %>%
    as.data.frame() %>%
    .$SampleID

allData <- read.csv(opt$i, header = T, stringsAsFactors = F) %>%
    select(- c("HMDB", "KEGG", "Metabolite")) %>%
    select(c("Class", sampleIds)) %>%
    group_by(Class) %>%
    summarise_all(sum)

head(allData)

parent <- "./"
fileName <- paste0(parent, "Class_Table.csv")
data <- allData
write.csv(data, fileName, row.names = F)


